package com.txby.platformApi.component.web;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jck.common.util.BeanToMapUtil;
import com.txby.pf.common.annotation.SysLog;
import com.txby.pf.common.utils.R;
import com.txby.pf.modules.sys.entity.SysDeptEntity;
import com.txby.pf.modules.sys.service.SysDeptService;
import com.txby.platformApi.component.service.SysDeptRegionRelationService;
import com.txby.platformCore.entity.SysDeptPerson;
import com.txby.platformCore.entity.SysDeptPersonRelation;
import com.txby.platformCore.entity.SysDeptRegionRelation;

/**
 * 代理商组织
 */
@RestController
@RequestMapping("/sys/area")
public class SysAgentController  extends CommonController {
	
	@Autowired
	private SysDeptRegionRelationService sysDeptRegionRelationService;
	@Autowired
	private SysDeptService sysDeptService;
	/**
	 * region区域
	 */
	@RequestMapping("/select")
	public R select(){
		String sql="select id,level,code,parentCode,regionName as name from sys_chinaRegion where `level` <= 3";
		List areaList=this.mBaseDao.findBySql(sql, Map.class, null);
		return R.ok().put("areaList", areaList);
	}
	
	
	
	/**
	 * 保存
	 * @throws Exception 
	 */
	@RequestMapping("/save")
	public R save(@RequestBody Map dept) throws Exception{
		// System.out.println(dept);
		if (dept.size()>0) {
		  Map map = new HashMap<>();//dept
		  Map map2 = new HashMap<>();//联系人map
		  Map map3 = new HashMap<>();//区域map
		  Map map4 = new HashMap<>();
		  
		  map.put("parentId",Long.parseLong(dept.get("parentId").toString()));
		  map.put("name", dept.get("name"));
		  map.put("type", Integer.parseInt((dept.get("type")).toString()));
		  map.put("orderNum", Integer.parseInt((dept.get("orderNum")).toString()));
		 
		  map2.put("linkname", dept.get("linkname"));//联系人
		  map2.put("duty", dept.get("duty"));//职务
		  map2.put("mobileNO", dept.get("mobileNO"));//手机号
		  
		  
		 
		  map3.put("address", dept.get("address"));
		  
		  
		  
		  SysDeptEntity sysDeptEntity =  (SysDeptEntity) BeanToMapUtil.convertMap(SysDeptEntity.class, map);
		  SysDeptPerson sysDeptPerson = (SysDeptPerson) BeanToMapUtil.convertMap(SysDeptPerson.class, map2);
		  SysDeptRegionRelation sysDeptRegionRelation = (SysDeptRegionRelation) BeanToMapUtil.convertMap(SysDeptRegionRelation.class, map3);
		  sysDeptPerson.setCreater(getUserId().toString());
		  sysDeptPerson.setUpdater(getUserId().toString());
		  
		  //System.out.println(JSON.toJSONString(sysDeptEntity));
		  sysDeptService.save(sysDeptEntity);
		  this.mBaseDao.saveObject(sysDeptPerson);
		  
		  map4.put("deptPersonId", sysDeptPerson.getId_());
		  
		  sysDeptRegionRelation.setDeptId(sysDeptEntity.getDeptId());
		  sysDeptRegionRelation.setCreater(getUserId().toString());
		  sysDeptRegionRelation.setUpdater(getUserId().toString());
		  sysDeptRegionRelation.setRegionId(new Long(dept.get("regionId").toString()));
		  this.mBaseDao.saveObject(sysDeptRegionRelation);
		  
		  
		  SysDeptPersonRelation sysDeptPersonRelation = (SysDeptPersonRelation) BeanToMapUtil.convertMap(SysDeptPersonRelation.class, map4);
		  sysDeptPersonRelation.setDeptId(sysDeptEntity.getDeptId());
		  sysDeptPersonRelation.setCreater(getUserId().toString());
		  sysDeptPersonRelation.setUpdater(getUserId().toString());
		  this.mBaseDao.saveObject(sysDeptPersonRelation);
		  
		}
		return R.ok();
	}
	
	/**
	 * 用户信息
	 */
	@RequestMapping("/info/{id}")
	public R info(@PathVariable("id") Long id){
		String sql="SELECT * FROM t_txby_sysDeptRegionRelation t1, sys_dept_personRelation t2, sys_dept t3, sys_chinaRegion t4, sys_dept_person t5 WHERE t1.deptId = t3.dept_id AND t1.regionId = t4.id_ AND t2.deptId = t1.deptId AND t2.deptPersonId = t5.id_ and t3.type = 6 and t3.dept_id ="+id;
		Map dept =  (Map) this.mBaseDao.findBySqlUniqueObject(sql, Map.class, null);
		return R.ok().put("dept", dept);
	}
	
	
	/**
	 * 修改
	 * @throws Exception 
	 * @throws InvocationTargetException 
	 * @throws InstantiationException 
	 * @throws IllegalAccessException 
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sys:area:update")
	public R update(@RequestBody Map dept) throws IllegalAccessException, InstantiationException, InvocationTargetException, Exception{
		if (dept.size()>0) {
			  Map map = new HashMap<>();//dept
			  Map map2 = new HashMap<>();//联系人map
			  Map map3 = new HashMap<>();//区域map
			  Map map4 = new HashMap<>();
			  
			  map.put("parentId",Long.parseLong(dept.get("parentId").toString()));
			  map.put("name", dept.get("name"));
			  map.put("type", Integer.parseInt((dept.get("type")).toString()));
			  map.put("orderNum", Integer.parseInt((dept.get("orderNum")).toString()));
			 
			  map2.put("linkname", dept.get("linkname"));//联系人
			  map2.put("duty", dept.get("duty"));//职务
			  map2.put("mobileNO", dept.get("mobileNO"));//手机号
			  
			  
			 
			  map3.put("address", dept.get("address"));
			  
			  
			  
			  SysDeptEntity sysDeptEntity =  (SysDeptEntity) BeanToMapUtil.convertMap(SysDeptEntity.class, map);
			  SysDeptPerson sysDeptPerson = (SysDeptPerson) BeanToMapUtil.convertMap(SysDeptPerson.class, map2);
			  SysDeptRegionRelation sysDeptRegionRelation = (SysDeptRegionRelation) BeanToMapUtil.convertMap(SysDeptRegionRelation.class, map3);
			  sysDeptPerson.setCreater(getUserId().toString());
			  sysDeptPerson.setUpdater(getUserId().toString());
			  
			  //System.out.println(JSON.toJSONString(sysDeptEntity));
			  sysDeptService.update(sysDeptEntity);
			  this.mBaseDao.updateObject(sysDeptPerson);
			  
			  map4.put("deptPersonId", sysDeptPerson.getId_());
			  
			  sysDeptRegionRelation.setDeptId(sysDeptEntity.getDeptId());
			  sysDeptRegionRelation.setCreater(getUserId().toString());
			  sysDeptRegionRelation.setUpdater(getUserId().toString());
			  sysDeptRegionRelation.setRegionId(new Long(dept.get("regionId").toString()));
			  this.mBaseDao.updateObject(sysDeptRegionRelation);
			  
			  
			  SysDeptPersonRelation sysDeptPersonRelation = (SysDeptPersonRelation) BeanToMapUtil.convertMap(SysDeptPersonRelation.class, map4);
			  sysDeptPersonRelation.setDeptId(sysDeptEntity.getDeptId());
			  sysDeptPersonRelation.setCreater(getUserId().toString());
			  sysDeptPersonRelation.setUpdater(getUserId().toString());
			  this.mBaseDao.updateObject(sysDeptPersonRelation);
			  
			}
			return R.ok();
	}
	
	/**
	 * 删除用户
	 */
	@SysLog("删除用户")
	@RequestMapping("/delete")
	@RequiresPermissions("sys:agent:delete")
	public R delete(@RequestBody Long[] ids){
		 for(Long id:ids){
		    	this.mBaseDao.executeSql("DELETE t1, t2, t3, t5 FROM t_txby_sysDeptRegionRelation t1, sys_dept_personRelation t2, sys_dept t3, sys_chinaRegion t4, sys_dept_person t5 WHERE t1.deptId = t3.dept_id AND t1.regionId = t4.id_ AND t2.deptId = t1.deptId AND t2.deptPersonId = t5.id_ AND t3.type = 6 AND t3.dept_id ="+id, null);
		  }
			return R.ok();
	}
	
	
	
}
